package pdf;
/** 
* @author 杨迅
* @number 16301024
* @time 2018年4月24日 下午10:05:55
* FileName:Merge.java
* Description:
*/
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; 

public class MergeAndSplit {
	/////////////////////////////////合并/////////////////////////////
	private static final int MAX_NUMBER = 100;//表示最多能一次合并的文件数
	private String[] inputFilepath = new String[MAX_NUMBER]; //用来储存添加的文件的路径和文件名
	private int number = 0;//添加的文件数
	private String outputFilepath = "";//输出文件的路径和文件名
	
	public void addFile(String filepath){
		this.inputFilepath[number] = filepath;
		number++;
	}
	public void setOutputFilepath(String filepath){
		this.outputFilepath = filepath;
	}
	@SuppressWarnings("deprecation")
	public void mergePDF() throws IOException{
	    //Instantiating PDFMergerUtility class 
		PDFMergerUtility PDFmerger = new PDFMergerUtility();  
	    //Setting the destination file 
	    PDFmerger.setDestinationFileName(outputFilepath); 
		for(int i = 0; i < number; i++){
			File file = new File(inputFilepath[i]);
		    //adding the source files 
		    PDFmerger.addSource(file); 
		}
	    //Merging those documents 
	    PDFmerger.mergeDocuments(); 
	}
	///////////////////////////////////拆分////////////////////////////////////
	public static final void splitPDF(String pdfFile, String newFile, int from, int end) throws IOException, DocumentException { 
	       Document document = null; 
	       PdfCopy copy = null;      
           PdfReader reader = new PdfReader(pdfFile);           
           int n = reader.getNumberOfPages();           
           if(end==0){ 
               end = n; 
           } 
           ArrayList<String> savepaths = new ArrayList<String>(); 
           String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1); 
           String savepath = staticpath+ newFile; 
           savepaths.add(savepath); 
           document = new Document(reader.getPageSize(1)); 
           copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0))); 
           document.open(); 
           for(int j=from; j<=end; j++) {
               document.newPage();
               PdfImportedPage page = copy.getImportedPage(reader, j);
               copy.addPage(page);
           }
           document.close();
	       
	}
//	public static void main(String[] args) throws IOException { 
//		MergeAndSplit m = new MergeAndSplit();
//		 m.addFile("E:/many1.pdf");
//		 m.addFile("E:/many2.pdf");
//		 m.addFile("E:/many3.pdf");
//		 m.setOutputFilepath("E:/many4.pdf");
//		 m.mergePDF();
//	} 
	
//	public static void main(String[] args) throws IOException, DocumentException { 
//		 MergeAndSplit m = new MergeAndSplit();
//		 m.splitPDF("E:/many1.pdf", "E:/many6.pdf", 2, 10);
//	}
}
